package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetMoneyChange;
import com.ruoyi.system.mapper.HatchetMoneyChangeMapper;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetMoneyChangeService;

/**
 * 提现记录 服务层实现
 * 
 * @author ruoyi
 * @date 2019-02-18
 */
@Service
public class HatchetMoneyChangeServiceImpl implements IHatchetMoneyChangeService 
{
	@Autowired
	private HatchetMoneyChangeMapper hatchetMoneyChangeMapper;
	@Autowired
	private IHatchetAgentService agentService;
	  
	/**
     * 查询提现记录信息
     * 
     * @param iD 提现记录ID
     * @return 提现记录信息
     */
    @Override
	public HatchetMoneyChange selectHatchetMoneyChangeById(String iD)
	{
	    return hatchetMoneyChangeMapper.selectHatchetMoneyChangeById(iD);
	}
	
	/**
     * 查询提现记录列表
     * 
     * @param hatchetMoneyChange 提现记录信息
     * @return 提现记录集合
     */
	@Override
	public List<HatchetMoneyChange> selectHatchetMoneyChangeList(HatchetMoneyChange hatchetMoneyChange)
	{
	    return hatchetMoneyChangeMapper.selectHatchetMoneyChangeList(hatchetMoneyChange);
	}
	
    /**
     * 新增提现记录
     * 
     * @param hatchetMoneyChange 提现记录信息
     * @return 结果
     */
	@Override
	public int insertHatchetMoneyChange(HatchetMoneyChange hatchetMoneyChange)
	{
	    return hatchetMoneyChangeMapper.insertHatchetMoneyChange(hatchetMoneyChange);
	}
	
	/**
     * 修改提现记录
     * 
     * @param hatchetMoneyChange 提现记录信息
     * @return 结果
     */
	@Override
	public int updateHatchetMoneyChange(HatchetMoneyChange hatchetMoneyChange)
	{
	    return hatchetMoneyChangeMapper.updateHatchetMoneyChange(hatchetMoneyChange);
	}

	/**
     * 删除提现记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteHatchetMoneyChangeByIds(String ids)
	{
		return hatchetMoneyChangeMapper.deleteHatchetMoneyChangeByIds(Convert.toStrArray(ids));
	}

	@Override
	public int changeStatus(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.updateHatchetMoneyChange(hatchetMoneyChange);
	}

	@Override
	public List<HatchetMoneyChange> selectFenRenChangeList(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.selectFenRenChangeList(hatchetMoneyChange);
	}

	/**
	 * @param agentNo 商户编号
	 * @param concat时间起始
	 * @param concat2时间截至
	 * @return 提现次数
	 */
	public int findCountByAgentNO(String agentNo,String now) {
		return hatchetMoneyChangeMapper.findCountByAgentNO(agentNo,now.concat(" 00:00:00"),now.concat(" 23:59:59"));
	}
	
	/**
	 * 执行提现操作
	 * @param agent
	 * @param money
	 * @return
	 */
	@Transactional
	public boolean doMoneyChange (HatchetAgent agent,String money) throws RuntimeException{
		int updateAgent = agentService.updateHatchetAgent(agent);
		//添加提现申请记录
		HatchetMoneyChange moneyChange = new HatchetMoneyChange();
		moneyChange.setID(StringUtils.randomUUID());
		moneyChange.setMoneyType("10B");
		moneyChange.setPaymentOrderId("");
		moneyChange.setSTATUS("10B");
		moneyChange.setTrxAmt(new BigDecimal(money));
		moneyChange.setUserId(agent.getAgentNo());
		moneyChange.setUserType("10A");
		moneyChange.setCreateTime( new Date());
		if (updateAgent>0) {
			int inserMoneyChange = hatchetMoneyChangeMapper.insertHatchetMoneyChange(moneyChange);
			if (inserMoneyChange>0) {
				return true;
			} else {
				throw new RuntimeException("提现记录创建失败");
			}
		}
		return false;
	}

	/**
	 * 查询代理商每天的收益
	 * @param hatchetMoneyChange
	 * @return
	 */
	public List<HatchetMoneyChange> selectFenRenChangeListByDay(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.selectFenRenChangeListByDay(hatchetMoneyChange);
	}

	/**
	 * 查询代理商每天总收益（不分组）
	 * @param hatchetMoneyChange
	 * @return
	 */
	public BigDecimal selectSumFenRenChangeListByDay(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.selectSumFenRenChangeListByDay(hatchetMoneyChange);
	}
	
	/**
	 * 查询代理商或者商户总收益
	 * @param userId 用户或者代理商编号
	 * @return
	 */
	public BigDecimal selectSumFenRenChangeByUserId(String userId) {
		BigDecimal selectSumFenRenByUserId = hatchetMoneyChangeMapper.selectSumFenRenByUserId(userId);
		if (StringUtils.isNull(selectSumFenRenByUserId)) {
			return new BigDecimal("0");
		} else {
			return selectSumFenRenByUserId;
		}
	}

	/**
	 * 收益分析（查询代理商或者商户对自己的贡献收益）
	 * @param hatchetMoneyChange
	 * @return
	 */
	public BigDecimal selectMoneyAnalyze(HatchetMoneyChange hatchetMoneyChange) {
		
		return hatchetMoneyChangeMapper.selectMoneyAnalyze(hatchetMoneyChange);
	}

	/**
	 * 查询分润总金额
	 * @param hatchetMoneyChange
	 * @return
	 */
	public BigDecimal selectSumMoney(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.selectSumMoney(hatchetMoneyChange);
	}

	/**
	 * 查询提现总金额
	 * @param hatchetMoneyChange
	 * @return
	 */
	public BigDecimal selectTixianSumMoney(HatchetMoneyChange hatchetMoneyChange) {
		return hatchetMoneyChangeMapper.selectTixianSumMoney(hatchetMoneyChange) ;
	}

	/**
	 * 查询总收益不包含升级
	 */
	public BigDecimal selectSumMoneyByMoneyType(HatchetMoneyChange moneyChange) {
		
		 BigDecimal selectSumMoneyByMoneyType = hatchetMoneyChangeMapper.selectSumMoneyByMoneyType(moneyChange);
		 if (StringUtils.isNull(selectSumMoneyByMoneyType)) {
			return new BigDecimal("0");
		}else {
			 return selectSumMoneyByMoneyType;
		}
		
	}
}
